package com.emip.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.emip.bean.AlarmConfigBean;
import com.emip.bean.RoleBean;
import com.emip.bean.SiteBean;
import com.emip.bean.UserBean;
import com.emip.bean.UserSitesBean;
import com.emip.dao.SiteDAO;
import com.emip.dao.UserDAO;
import com.emip.dao.UserSitesDAO;
import com.emip.service.UserService;

@Service("userService")
public class UserServiceImpl implements UserService{
	@Autowired
	UserDAO userDAO;
	
	@Autowired
	UserSitesDAO userSitesDAO;

	@Autowired
	SiteDAO siteDAO;
	
	public UserBean login(String id, String password) {
		password = DigestUtils.md5Hex(password);
		UserBean result = userDAO.login(id, password);
		return result;
	}

	public UserBean addUser(String id, String name, String password,
			RoleBean role, String mobilePhone, String mailAddress, String flag,
			String smsFlag, String controllFlag, String smtp,
			String emailAccount, String emailPassword, AlarmConfigBean alarm,
			double longitude, double latitude, int mapZoom) {
		UserBean newBean = new UserBean();
		newBean.setId(id);
		newBean.setName(name);
		newBean.setPassword(DigestUtils.md5Hex(password));
		newBean.setRole(role);
		newBean.setMobilePhone(mobilePhone);
		newBean.setMailAddress(mailAddress);
		newBean.setSmsFlag(smsFlag);
		newBean.setFlag(flag);
		newBean.setControllFlag(controllFlag);
		newBean.setSmtp(smtp);
		newBean.setEmailAccount(emailAccount);
		newBean.setEmailPassword(emailPassword);
		newBean.setAlarm(alarm);
		newBean.setLongitude(longitude);
		newBean.setLatitude(latitude);
		newBean.setMapZoom(mapZoom);
		//查重，是否已经有相同的ID的记录存在
		//若有重复记录，则返回null
		if (userDAO.findExist(newBean, 1)) {
			return null;
		}
		userDAO.add(newBean);
		return newBean;
	}

	public Boolean deleteUser(String id) {
		UserBean delBean = userDAO.findByID(id);
		userDAO.remove(delBean);
		return true;
	}

	public List<UserBean> getAll() {
		// TODO Auto-generated method stub
		return userDAO.getAll();
	}

	public UserBean getUserByID(String id) {
		// TODO Auto-generated method stub
		UserBean bean = userDAO.findByID(id);
		bean.setPassword("secret");
		return bean;
	}

	public UserBean updateUser(String id, String name, String password,
			RoleBean role, String mobilePhone, String mailAddress, String flag,
			String smsFlag, String controllFlag, String smtp,
			String emailAccount, String emailPassword, AlarmConfigBean alarm,
			double longitude, double latitude, int mapZoom) {
		UserBean newBean = userDAO.findByID(id);
		newBean.setName(name);
		//修改的时候不再将password修改
		//newBean.setPassword(DigestUtils.md5Hex(password));
		newBean.setRole(role);
		newBean.setMobilePhone(mobilePhone);
		newBean.setMailAddress(mailAddress);
		newBean.setSmsFlag(smsFlag);
		newBean.setFlag(flag);
		newBean.setControllFlag(controllFlag);
		newBean.setSmtp(smtp);
		newBean.setEmailAccount(emailAccount);
		newBean.setEmailPassword(emailPassword);
		newBean.setAlarm(alarm);
		newBean.setLongitude(longitude);
		newBean.setLatitude(latitude);
		newBean.setMapZoom(mapZoom);
		//查重，是否已经有相同的ID的记录存在
		//若有重复记录，则返回null
		if (userDAO.findExist(newBean, 2)) {
			return null;
		}
		userDAO.update(newBean);
		return newBean;
	}

	public List<SiteBean> getUserSites(String userid) {
		List<UserSitesBean> uSites = userSitesDAO.getSitesByUser(userid);
		List<SiteBean> result = new ArrayList<SiteBean>();
		for (int i=0;i<uSites.size(); i++) {
			UserSitesBean b = uSites.get(i);
			result.add(b.getSite());
		}
		return result;
	}

	public boolean setUserSites(String userID, String[] siteIDs) {
		userSitesDAO.deleteUserSites(userID);
		UserSitesBean newBean = null;
		for (String siteID : siteIDs) {
			newBean = new UserSitesBean();
			newBean.setUserid(userID);
			SiteBean sBean = siteDAO.findByID(siteID);
			newBean.setSite(sBean);
			userSitesDAO.add(newBean);
		}
		return true;
	}

	public UserBean updateUserSimple(String id, String name,
			String mobilePhone, String mailAddress, double longitude,
			double latitude, int mapZoom) {
		UserBean newBean = userDAO.findByID(id);
		newBean.setName(name);
		//修改的时候不再将password修改
		//newBean.setPassword(DigestUtils.md5Hex(password));
		newBean.setMobilePhone(mobilePhone);
		newBean.setMailAddress(mailAddress);
		newBean.setLongitude(longitude);
		newBean.setLatitude(latitude);
		newBean.setMapZoom(mapZoom);
		//查重，是否已经有相同的ID的记录存在
		//若有重复记录，则返回null
		if (userDAO.findExist(newBean, 2)) {
			return null;
		}
		userDAO.update(newBean);
		return newBean;
	}

	public boolean isUserPwd(String userID, String pwd) {
		UserBean newBean = userDAO.findByID(userID);
		String oriPwd = newBean.getPassword();
		String pwd2 = DigestUtils.md5Hex(pwd);
		return oriPwd.equals(pwd2);
	}

	public UserBean updateUserPwd(String userID, String newPwd) {
		UserBean newBean = userDAO.findByID(userID);
		newBean.setPassword(DigestUtils.md5Hex(newPwd));
		if (userDAO.findExist(newBean, 2)) {
			return null;
		}
		userDAO.update(newBean);
		return newBean;
	}
}
